package org.finesys.common.hazelcast.core.support;

import java.util.concurrent.locks.Lock;

import org.finesys.common.hazelcast.core.base.DistributedBaseFactory;
import org.finesys.common.hazelcast.core.base.DistributedCountDownLatch;
import org.finesys.common.hazelcast.core.base.DistributedMap;
import org.finesys.common.hazelcast.core.base.DistributedQueue;
import org.finesys.common.hazelcast.core.base.DistributedSet;

import com.hazelcast.core.HazelcastInstance;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class HazecastBaseFactory implements DistributedBaseFactory {

    private final HazelcastInstance instance;

    /**
     * 获取分布式集合
     *
     * @param name 分布式集合的名称
     * @return 分布式集合
     */
    @Override
    public DistributedCountDownLatch getCountDownLatch(String name) {
        return new HazelcastCountDownLatch(instance.getCPSubsystem().getCountDownLatch(name));
    }

    /**
     * 获取分布式集合
     *
     * @param name 分布式集合的名称
     * @return 分布式集合
     */
    @Override
    public Lock getLock(String name) {
        return instance.getCPSubsystem().getLock(name);
    }

    /**
     * 获取分布式集合
     *
     * @param name 分布式集合的名称
     * @return 分布式集合
     */
    @Override
    public <K, V> DistributedMap<K, V> getMap(String name) {
        return new HazelcastMap<>(instance.getMap(name));
    }

    @Override
    public <T> DistributedQueue<T> getQueue(String name) {
        return new HazelcastQueue<>(instance.getQueue(name));
    }

    @Override
    public <T> DistributedSet<T> getSet(String name) {
        return new HazelcastSet<>(instance.getSet(name));
    }
}
